/**
  ******************************************************************************
  * @file    main.c 
  * @author  gzkk
  * @version V1.0.0
  * @date    2024-11-25
  * @brief   工程模版（stm32f103VET6开发板）
  ******************************************************************************
  * @attention
  *
  * 
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>


#include "usart.h"
#include "oled.h"
#include "bmp.h"
#include "hc_sr04.h"
#include "dht11.h"
#include "adc.h"
#include "sg90.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
extern volatile uint8_t  uart2_recvbuf[512];
extern volatile uint32_t uart2_recvcnt;
extern volatile uint32_t uart2_recvflag;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile uint32_t flag=0;

/* Private function prototypes -----------------------------------------------*/


/* Private functions ---------------------------------------------------------*/

void Config(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //抢占优先级范围0~15
	USART1_Init(9600);
	USART2_Init(9600);
	USART3_Init(115200);
	Hc_Sr04_Init(1000,72);
	OLED_Init();
	OLED_Clear();
	DHT11_Config();
	printf("init ok\r\n");
	
	OLED_DrawBMP(30,1,90,6,WIFI);
	esp8266_Config();
	delay_s(2);
}

int main(void)
{

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //抢占优先级范围0~15
	Config();
	sg90_init();
	sg90_reset();
	delay_s(3);
	OLED_DrawBMP(40,0,88,6,BMP2);
	
	while (1)
	{	
		float val=0;
		if(uart2_recvflag == 1)
		{
		uart2_recvflag = 0;
		if(strstr((char*)uart2_recvbuf,"move"))
		{		
			memset((char *)uart2_recvbuf,0,BUFFERSIZE);		//清空缓冲区
			uart2_recvcnt = 0;	
			while(1)
			{
											
				if(uart2_recvflag == 1)
				{
					uart2_recvflag = 0;
					if(strstr((char*)uart2_recvbuf,"move"))
					{
						memset((char *)uart2_recvbuf,0,BUFFERSIZE);		//清空缓冲区
						uart2_recvcnt = 0;								//清空计数器
						break;
					}
					if(strstr((char*)uart2_recvbuf,"before"))
					{
						before();
						sg90_reset();
					}	
					if(strstr((char*)uart2_recvbuf,"left"))
					{
						lift();
						sg90_reset();
					}	
					if(strstr((char*)uart2_recvbuf,"down"))
					{
						down();
						sg90_reset();
					}
					if(strstr((char*)uart2_recvbuf,"right"))
					{
						right();
						sg90_reset();
					}
					memset((char *)uart2_recvbuf,0,BUFFERSIZE);		//清空缓冲区
					uart2_recvcnt = 0;								//清空计数器
				}
			
			}
			sg90_reset();
		
		}
		if(strstr((char*)uart2_recvbuf,"hand"))
		{
			left_hand();
			delay_s(3);
			sg90_reset();
			
		}
		if(strstr((char*)uart2_recvbuf,"dance"))
		{
			Dancing();
			sg90_reset();
		}
		if(strstr((char*)uart2_recvbuf,"get"))
		{
			uint32_t a=0;
			OLED_Clear();
			a=esp8266_getweather("SagCiKz43PYKIdUV2", "guangzhou");
			OLED_ShowCHinese(0,0,0);
			OLED_ShowCHinese(20,0,1);
			OLED_ShowString(40,0,buf_get,strlen(buf_get));
			OLED_ShowCHinese(0,2,2);
			OLED_ShowCHinese(18,2,3);
			OLED_DrawBMP(80,0,128,6,BMP4);
			OLED_ShowNum(40,2,a,2,16);
		}
		if(strstr((char*)uart2_recvbuf,"dht"))
		{
			fun1();
		}
		memset((char *)uart2_recvbuf,0,BUFFERSIZE);		//清空缓冲区
		uart2_recvcnt = 0;								//清空计数器
		}
		
		val=GetLength();
		if(val<5)
		{
			if(flag==0)
			{
				flag=1;
				OLED_Clear();
				OLED_DrawBMP(32,0,96,7,BMP3);
			}
			else
			{
				flag=0;
				OLED_Clear();
				OLED_DrawBMP(40,0,88,6,BMP2);
			}
			
		}

  }
}

///**
//  * @brief  温湿度检测
//  * @param  None
//  * @retval None
// */
//int main(void)
//{
//	
//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //抢占优先级范围0~15
//	USART1_Init(9600);
//	sg90_init();
//	sg90_reset();
//	delay_s(2);
	
	
//	PWM_PUT(120,GPIO_Pin_2);	//右脚舵机
//	PWM_PUT(80,GPIO_Pin_	1);		//左脚舵机
//	delay_s(1);
//	PWM_PUT(120,GPIO_Pin_3);  	//右内舵机                                                            
//	delay_s(1);
//	PWM_PUT(90,GPIO_Pin_2);	 	//右脚舵机
//	delay_ms(500);
//	PWM_PUT(60,GPIO_Pin_1);		//左脚舵机
//	PWM_PUT(90,GPIO_Pin_4);
//	delay_s(1);
//	PWM_PUT(90,GPIO_Pin_3);  	//右内舵机
//	PWM_PUT(90,GPIO_Pin_1);		//左脚舵机
//	delay_s(1);
	
	
//	while (1)
//	{	
//		
//		
//	}
//}


//	PWM_PUT(120,GPIO_Pin_4);  	//右内舵机   
//	PWM_PUT(120,GPIO_Pin_3);  	//左内舵机
//	PWM_PUT(70,GPIO_Pin_2);	  	//右脚舵机
//	PWM_PUT(80,GPIO_Pin_1);		//左脚舵机
//	delay_s(2);
//	PWM_PUT(90,GPIO_Pin_2);
//	PWM_PUT(70,GPIO_Pin_1);
//	PWM_PUT(90,GPIO_Pin_4);
//	PWM_PUT(90,GPIO_Pin_3);
//	delay_s(2);

/**
  * @brief  温湿度检测
  * @param  None
  * @retval None
 
int main(void)
{
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //抢占优先级范围0~15
	USART1_Init(9600);
	Adc_Init();
	while (1)
	{	
		u16 val=0;
		val=Get_Adc_Average(ADC_Channel_1,5);
		printf("val = %d\r\n",val);
		delay_s(2);
	}
}
*/
/**
  * @brief  温湿度检测
  * @param  None
  * @retval None
  
int main(void)
{
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //抢占优先级范围0~15
	USART1_Init(9600);
	DHT11_Config();
	while (1)
	{	
		uint8_t val[8]={0};
		if(DHT11_GetVal(val))
		{
			printf("%d,%d\r\n",val[2],val[0]);
		}
		
		delay_s(2);
	}
}
*/
